import wx
import sys
import time
import wx.grid as grid
import threading



from xGridTable import StudentInfoGridTable


# menu = {'MenuScan': True, 'AutoScan': False, 'SemiScan': False, 'No SN': False, 'Debug': False, 'CheckSN': False}
choosemodel = ""





class ModelChoose(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "ModelChoose", size=(330, 200), pos=(400, 220))
        panel = wx.Panel(self)
        modellist = []

        """读取config里的所有model名"""
        f = open(r'C:\Users\admin\Desktop\wxPyton\ModelConfig.txt', 'r')
        modelname = f.read()
        f.close()
        list2=modelname.split('"')[1].split(',')


        self.modellist=wx.StaticText(panel,-1,"Model ",size=(40,30),pos=(40,40))

        self.modeltext= wx.Choice(panel, -1, (80, 40),(180, 40),list2)
        self.modeltext.Bind(wx.EVT_CHOICE,self.modellistclick)

        self.okbutton=wx.Button(panel,-1,"OK",size=(60,30),pos=(90,90))
        # self.okbutton.Bind(wx.EVT_BUTTON,self.okbuttonclick)



        self.cancelbutton = wx.Button(panel, -1, "Cancel", size=(60, 30), pos=(190, 90))
        self.cancelbutton.Bind(wx.EVT_BUTTON,self.cancelbuttonclick)

    def modellistclick(self,event):
        self.tempmodelname=self.modeltext.GetStringSelection()





    def cancelbuttonclick(self,event):
        self.Close()


class LogPath(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "LogPath", size=(300, 200), pos=(400, 220))
        panel = wx.Panel(self)

class ManuScan(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "ManuScan", size=(300, 200), pos=(400, 220))
        panel = wx.Panel(self)

class SimpleGrid(wx.grid.Grid):
    def __init__(self, parent,rownum=50):
        # wx.grid.Grid.__init__(self, parent, -1)
        wx.grid.Grid.__init__(self, parent ,-1 ,size=(700, 420),pos=(40,110))

        """设置Grid的大小"""
        self.CreateGrid(rownum, 6)

        """disable x scroll"""


        # for num in range(5):
        #     self.SetColSize(col=num,width=120)
        self.SetColSize(col=0, width=240)
        self.SetColSize(col=1, width=100)
        self.SetColSize(col=2, width=100)
        self.SetColSize(col=3, width=95)
        self.SetColSize(col=4, width=120)

        #设置第index=4行的高度大小，像素=45
        # self.SetRowSize(4, 45)
        self.RowLabelSize = 30

        self.SetColLabelValue(0, "TestItem")
        self.SetColLabelValue(1, "LowLimit")
        self.SetColLabelValue(2, "UpperLimit")
        self.SetColLabelValue(3, "Mearuse")
        self.SetColLabelValue(4, "Result")
        self.SetColLabelValue(5, "Mark")



        # self.SetCellValue(0, 0, "Bob")
        # self.SetCellValue(0, 1, "1111111111111111111")
        #
        # self.SetCellValue(1, 0, "Ryne")
        # self.SetCellValue(1, 1, "Sandberg")
        #
        # self.SetCellValue(2, 0, "Gary")
        # self.SetCellValue(2, 1, "Matthews")
        #
        # self.SetCellValue(3, 0, "Leon")
        # self.SetCellValue(3, 1, "Durham")
        #
        # self.SetCellValue(4, 0, "Keith")
        # self.SetCellValue(4, 1, "Moreland")
        #
        # self.SetCellValue(5, 0, "Ron")
        # self.SetCellValue(5, 1, "Cey")
        #
        # self.SetCellValue(6, 0, "Jody")
        # self.SetCellValue(6, 1, "Davis")
        #
        # self.SetCellValue(7, 0, "Larry")
        # self.SetCellValue(7, 1, "Bowa")
        #
        # self.SetCellValue(8, 0, "Rick")
        # self.SetCellValue(8, 1, "Sutcliffe")

    # def setdata(self,row,column,data):
    #     self.row=row
    #     self.column=column
    #     self.data=data
    #     self.SetCellValue(row, column, data)


class MainUI(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self,None,88,"FX-MainUI",size=(880,610),pos=(150,60))
        panel = wx.Panel(self)

        """build threads"""
        self.threads = []

        self.grid = SimpleGrid(panel)


        """进度条展示"""
        self.gauge1 = wx.Gauge(panel, -1, 100, size=(805, 13), pos=(40, 80))
        self.value = 1

        """model"""
        self.modelstatictext=wx.StaticText(panel,-1,"Model ",size=(40,30),pos=(40,20))
        self.modeltext= wx.TextCtrl(panel, -1, "Model", size=(150, 20), pos=(80, 20))
        self.modeltext.SetValue("ddd")

        """SN"""
        self.modelstatictext=wx.StaticText(panel,-1,"S/N ",size=(40,30),pos=(40,50))
        self.sntext= wx.TextCtrl(panel, -1, "sn", size=(150, 20), pos=(80, 50))

        """PASS"""
        self.modelstatictext=wx.StaticText(panel,-1,"Pass ",size=(40,30),pos=(260,20))
        self.passtext= wx.TextCtrl(panel, -1, "PASS", size=(80, 20), pos=(300, 20))

        """FAIL"""
        self.modelstatictext=wx.StaticText(panel,-1,"Fail",size=(40,30),pos=(260,50))
        self.failtext= wx.TextCtrl(panel, -1, "fail", size=(80, 20), pos=(300, 50))

        """Time"""
        self.modelstatictext=wx.StaticText(panel,-1,"Time ",size=(40,30),pos=(410,20))
        self.timetext= wx.TextCtrl(panel, -1, "time", size=(80, 20), pos=(450, 20))



        """Status"""
        self.statustext= wx.TextCtrl(panel, -1, "Testing", size=(130, 50), pos=(560, 20))

        bm = wx.Bitmap('C:\\Users\\admin\\Desktop\\wxPyton\\LOG.jpg', wx.BITMAP_TYPE_ANY)


        self.log = wx.StaticBitmap(panel, -1,bm, size=(145,70), pos=(700,0))


        """startButton"""
        self.startbutton=wx.Button(panel,-1,"Start",size=(80,40),pos=(765,110))
        self.startbutton.Bind(wx.EVT_BUTTON,self.startbuttonclick)

        """stopButton"""
        self.stopbutton=wx.Button(panel,-1,"Stop",size=(80,40),pos=(765,190))
        self.stopbutton.Bind(wx.EVT_BUTTON,self.stopbuttonclick)

        """ExitButton"""
        self.exitbutton=wx.Button(panel,-1,"Exit",size=(80,40),pos=(765,490))
        self.exitbutton.Bind(wx.EVT_BUTTON, self.exitbuttonclick)



        """创建一个菜单栏"""
        menubar = wx.MenuBar()
        """创建filemenu  settingmenu菜单"""
        self.filemenu = wx.Menu()
        self.settingmenu = wx.Menu()


        self.filemenu.Append(id=1,item="CheckModel")

        self.settingmenu.Append(id=10, item="LogPath")
        self.settingmenu.AppendSeparator()
        self.settingmenu.AppendRadioItem(id=11, item="ManuScan")
        self.settingmenu.AppendRadioItem(id=12, item="AutoScan")
        self.settingmenu.AppendRadioItem(id=13, item="SemiScan")
        self.settingmenu.AppendSeparator()
        self.settingmenu.AppendCheckItem(id=14, item="No SN")
        self.settingmenu.AppendCheckItem(id=15, item="Debug")
        self.settingmenu.AppendCheckItem(id=16, item="CheckSN")

        """将菜单加载到菜单栏上"""
        menubar.Append(self.filemenu, title="File")
        menubar.Append(self.settingmenu, title="&Config")

        """将菜单栏加载到UI上"""
        self.SetMenuBar(menubar)

        """绑定事件处理"""
        self.Bind(wx.EVT_MENU, self.menuHandler)

        print(self.GetMenuBar())

    def menuHandler(self, event):

        id = event.GetId()
        if id == 1:
            print("1")
            self.modelchoose = ModelChoose()
            self.modelchoose.Show(True)
            self.modelchoose.okbutton.Bind(wx.EVT_BUTTON,self.modelchooseokbuttonclick)

        # if id == 10:
        #     print(menu['No SN'])
        #     self.myframe2 = LogPath()
        #     self.myframe2.Show(True)
        #
        # if id == 11:
        #     menu['MenuScan'] = True
        #     menu['AutoScan'] = False
        #     menu['SemiScan'] = False
        #     print("MenuScan 11 :")
        #     print(menu['MenuScan'])
        #
        #
        # if id == 12:
        #     menu['MenuScan'] = False
        #     menu['AutoScan'] = True
        #     menu['SemiScan'] = False
        #     print("AutoScan 12 :")
        #     print(menu['AutoScan'])
        #
        #
        # if id == 13:
        #     menu['MenuScan'] = False
        #     menu['AutoScan'] = False
        #     menu['SemiScan'] = True
        #     print("SemiScan 13 :")
        #     print(menu['AutoScan'])
        #
        #
        #
        # if id == 14:
        #     menu['No SN'] =not menu['No SN']
        #     print("No SN 14:")
        #     print(menu['No SN'])
        # if id == 15:
        #     menu['Debug'] =not menu['Debug']
        #     print("15")
        #     print(menu['Debug'])
        # if id == 16:
        #     menu['CheckSN'] = not menu['CheckSN']
        #     print("16")
        #     print(menu['CheckSN'])
        # return id

    def modelchooseokbuttonclick(self, event):
        modelname=self.modelchoose.modeltext.GetStringSelection()

        """复位scroll"""
        self.grid.Scroll(0,0)

        """初始化前面板"""
        self.modeltext.SetValue(modelname)
        # self.modeltext.SetFont(40)

        self.statustext.SetValue("Ready")
        # self.statustext.SetFont(wx.Font)

        """clear the test list and background colour of display"""
        self.grid.ClearGrid()
        for i in range(self.grid.GetNumberRows()):
            for j in range(5):
                self.grid.SetCellBackgroundColour(i,j,wx.WHITE)


        """read the sect model test list"""
        path="C:\\Users\\admin\Desktop\\wxPyton\\" + modelname + ".txt"
        f = open(path, 'r')
        testlist=f.read()
        testitem = testlist.split()


        self.testlistflag = []
        self.testlistname = []
        self.testlistlimit = []


        print(len(testitem))
        for i in range(len(testitem)):
            # print(str(i) + ":  " + testitem[i])
            testrow = testitem[i].split("#")
            self.testlistflag.append(testrow[0])
            self.testlistname.append(testrow[1])
            self.testlistlimit.append(testrow[2])
            # print(testrow[0])
            self.grid.SetCellValue(i, 0, testrow[1])
            self.grid.SetCellValue(i, 1, testrow[2])
            if testrow[0] == "F":
                self.grid.SetCellBackgroundColour(i, 0, wx.YELLOW)
                self.grid.SetCellBackgroundColour(i, 1, wx.YELLOW)
                self.grid.SetCellBackgroundColour(i, 2, wx.YELLOW)
                self.grid.SetCellBackgroundColour(i, 3, wx.YELLOW)
                self.grid.SetCellBackgroundColour(i, 4, wx.YELLOW)



        # print(self.testlistflag)
        # print(self.testlistlimit)
        # print(self.testlistname)

        self.modelchoose.Close()


    def exitbuttonclick(self,event):
         self.Close()

    def startbuttonclick(self,event):
        # if self.== True:
        #     print("menuscan")
        # if menu['AutoScan'] == True:
        #     print("AutoScan")
        #
        # if menu['SemiScan'] == True:
        #      print("SemiScan")

        # print("---------------------------------")
        # print("11 :",self.settingmenu.IsChecked(11))
        # print("12 :",self.settingmenu.IsChecked(12))
        # print("13 :",self.settingmenu.IsChecked(13))
        # print("14 :",self.settingmenu.IsChecked(14))
        # print("15 :",self.settingmenu.IsChecked(15))
        # print("16 :",self.settingmenu.IsChecked(16))
        # print("---------------------------------")

        self.startbutton.Enable(False)

        """复位scroll"""
        self.grid.Scroll(0,0)

        """clear the test list and background colour of display"""
        # self.grid.ClearGrid()
        for i in range(self.grid.GetNumberRows()):
            if self.grid.GetCellBackgroundColour(i,0) != wx.YELLOW:
                self.grid.SetCellBackgroundColour(i, 0, wx.WHITE)

            if self.grid.GetCellBackgroundColour(i, 1) != wx.YELLOW:
                self.grid.SetCellBackgroundColour(i,1,wx.WHITE)

            if self.grid.GetCellBackgroundColour(i, 2) != wx.YELLOW:
                self.grid.SetCellBackgroundColour(i, 2, wx.WHITE)

            if self.grid.GetCellBackgroundColour(i, 3) != wx.YELLOW:
                self.grid.SetCellBackgroundColour(i, 3, wx.WHITE)

            if self.grid.GetCellBackgroundColour(i, 4) != wx.YELLOW:
                self.grid.SetCellBackgroundColour(i, 4, wx.WHITE)

            self.grid.SetCellValue(i,3,"")
            self.grid.SetCellValue(i, 4, "")


        flag_menuscan = self.settingmenu.IsChecked(11)
        flag_autoscan = self.settingmenu.IsChecked(12)
        flag_semiscan = self.settingmenu.IsChecked(13)
        flag_nosn = self.settingmenu.IsChecked(14)
        flag_debug = self.settingmenu.IsChecked(15)
        flag_checksn = self.settingmenu.IsChecked(16)

        # if flag_menuscan == True:
        #     print("menuscan")
        # if flag_autoscan == True:
        #     print("autoscan")
        # if flag_semiscan == True:
        #     print("semiscan")
        #
        #
        # if flag_nosn == True:
        #     print("nosn")
        # if flag_debug == True:
        #     print("debug")
        # if flag_checksn == True:
        #     print("checksn")


        n=len(self.testlistname)
        for i in range(n) :

            # print(self.testlistname[i])
            if self.testlistflag[i] == "T":
                if self.testlistname[i] == "delay":
                    # self.grid.Update()
                    # s=self.testlistlimit[i]
                    # print(s)
                    # time.sleep(1)
                    self.grid.SetCellValue(i,4,"pass")
                    self.grid.SetCellBackgroundColour(i, 0, wx.GREEN)
                    self.grid.SetCellBackgroundColour(i, 1, wx.GREEN)
                    self.grid.SetCellBackgroundColour(i, 2, wx.GREEN)
                    self.grid.SetCellBackgroundColour(i, 3, wx.GREEN)
                    self.grid.SetCellBackgroundColour(i, 4, wx.GREEN)
                    # self.grid.Update()

                    # time.sleep(1)
                    for k in range(1000):
                        for j in range(1000):
                            a=100
                            b=a+20




                else:
                    # s = self.testlistlimit[i]
                    # print(s)
                    # time.sleep(1)
                    self.grid.SetCellValue(i, 4, "Fail")
                    self.grid.SetCellBackgroundColour(i, 0, wx.RED)
                    self.grid.SetCellBackgroundColour(i, 1, wx.RED)
                    self.grid.SetCellBackgroundColour(i, 2, wx.RED)
                    self.grid.SetCellBackgroundColour(i, 3, wx.RED)
                    self.grid.SetCellBackgroundColour(i, 4, wx.RED)

                self.grid.Update()

                if i > 15:
                    # print(i)
                    self.grid.Scroll(0,i-5)
            else:
                pass

        self.startbutton.Enable()


    def stopbuttonclick(self,event):
         print("stop test")


class MyApp(wx.App):
  def OnInit(self):

    self.myframe = MainUI()

    # self.myframe2 = ModelChoose()
    self.SetTopWindow(self.myframe)

    self.myframe.Show(True)

    return True


if __name__=='__main__':
  app = MyApp()
  app.MainLoop()
